<?php
/**
 * 拼团模型
 * 20170105周家财添加
 */
defined('InShopNC') or exit('Access Invalid!');
class groupbuy_memModel extends Model {
    public function __construct(){
        parent::__construct('groupbuy_mem');
    }
    
    /**
     * 查找正在参团的总数
     * 
     */
    public function get_mem_current_count($condition){
        $mem_count=0;
        //接收到用户id
        /* $condition['lead_flag']=0; */
        $condition['pay_status']=1;
        $member_list=$this->member_list($condition);
        $model_groupbuy_team=Model('groupbuy_team');
        
        foreach ($member_list as $key =>$val){
            $condition_team['team_id']=$val['team_id'];
            $team_info=$model_groupbuy_team->groupbuy_team_info($condition_team);    
            if (($team_info['team_number'] >$team_info['record_num'] ) && $team_info['end_time'] > time()){
                $mem_count++;
            }
        }
        
        return  $mem_count;
        //return $this->table('groupbuy_mem')->where($condition)->count();
        
    }
    public function member_list($condition){
        $member_list = Model()->table('groupbuy_mem')->where($condition)->order('mem_id desc')->limit(false)->select();
        if($member_list != null && !empty($member_list)){
            //用户模型
            $member_model=Model('member');
            
            //订单模型
            $order_model=Model('order');
            $condition_or['team_id']=$condition['team_id'];
            
            //拼团信息
            $groupbuy_team_model=Model('groupbuy_team');
            $groupbuy_team_info=$groupbuy_team_model->groupbuy_team_info($condition);
            
            foreach ($member_list as $key =>$val){
            
                $condition_member['member_id']=$condition_or['buyer_id']=$member_list[$key]['member_id'];
                //获取订单信息
                $order_info=$order_model->getOrderInfo($condition_or);
                //获取用户信息
                $member_info=$member_model->getMemberInfo($condition_member);
                $member_list[$key]['member_name']=$member_info['member_mobile'];
                //获取用户头像
                if (!empty($member_info['member_avatar'])){
                    $member_list[$key]['mem_avatar']=getMemberAvatarForFilePath($member_info['member_avatar']);
                }else{
                    $member_list[$key]['mem_avatar']=null;
                }
                $member_list[$key]['start_time']=$order_info['add_time'];
                $member_list[$key]['end_time']=$groupbuy_team_info['end_time'];
            }
                
            //$member_list[$key]['member_url']=urlShop('member', 'home',array('team_id'=>$mem_list[$key]['member_id']));
            
        }
        

        return $member_list;
    }
    
    public function groupbuy_mem_info($condition){
        $groupbuy_mem_info = Model()->table('groupbuy_mem')->where($condition)->find();
        if($groupbuy_mem_info !=null && !empty($groupbuy_mem_info)){
            $member_model=Model('member');
            $condition_member['member_id']=$groupbuy_mem_info['member_id'];
            $member_info=$member_model->getMemberInfo($condition_member);
            $groupbuy_mem_info['member_name']=$member_info['member_mobile'];
            //获取用户头像
            if (!empty($member_info['member_avatar'])){
                $groupbuy_mem_info['mem_avatar']=getMemberAvatarForFilePath($member_info['member_avatar']);
            }else{
                $groupbuy_mem_info['mem_avatar']=null;
            }
        }else{
            $groupbuy_mem_info=null;
        }
        

        return $groupbuy_mem_info;
    }
    
    public function check_mem_join($condition){
        return $this->table('groupbuy_mem')->where($condition)->count();
    }
    
    
    public function save_mem($data){
        return $this->table('groupbuy_mem')->insert($data);
    }
    
    public function delete_mem($where){
         return $this->table('groupbuy_mem')->where($where)->delete();
    }
    //更新
    public function update_mem($condition,$update){
        $result=Model()->table('groupbuy_mem')->where($condition)->update($update);
    
        return $result;
        //Model()->table('groupbuy_team')->where($condition)->order('team_id desc')->limit(false)->select();
    }
    
   
    
    //跑马灯参团
    public function GetCycleMem($groupbuy_id){
        if ($groupbuy_id!=0){
            $sql="select mem.mem_id,mem.team_id, team.groupbuy_id, mem.member_id, mem.lead_flag from lxs_groupbuy_mem as mem
                left join lxs_groupbuy_team as team on mem.team_id = team.team_id where (mem.pay_status>0 and team.groupbuy_id=".$groupbuy_id.") order by mem.mem_id desc";  
        }else{
            $sql="select mem.mem_id,mem.team_id,team.groupbuy_id, mem.member_id, mem.lead_flag from lxs_groupbuy_mem as mem
                left join lxs_groupbuy_team as team on mem.team_id = team.team_id where mem.pay_status>0   order by mem.mem_id desc ";
        }
       
        $mem_list=Model()->query($sql);
        $member_model=Model('member');
        if($mem_list !=null && !empty($mem_list)){
            foreach ($mem_list as $key=>$val){
                $condition['member_id']=$val['member_id'];
                $member_info=$member_model->getMemberInfo($condition);
                $member_name=substr_replace($member_info['member_mobile'],"****",3,4);
                $mem_list[$key]['mem_name']=$member_name;
            }

            //$member_list = Model()->table('groupbuy_mem')->where($condition)->order('mem_id desc')->limit(3)->select();
            return $mem_list;
        }
        
        
    }
    
    
   
}